import PlatformWebSocketServer from "@/utils/websocketServer";

const wsServer = PlatformWebSocketServer.getInstance();

// 注册 "test-request" 处理器
wsServer.registerHandler("test-request", async (payload, ws) => {
  // 1. 参数验证
  if (!payload.testId) {
    throw new Error("Missing testId");
  }

  // 2. 模拟处理测试任务
  console.log(`[Platform] Starting test ${payload.testId}...`);

  // 3. 主动推送进度（事件通知模式）
  for (let i = 1; i <= 3; i++) {
    await new Promise((resolve) => setTimeout(resolve, 1000)); // 模拟耗时操作
    wsServer.sendToClient(ws, {
      type: "test-progress",
      data: `Step ${i}/3 completed`,
    });
  }

  // 4. 返回最终结果
  return { result: `Test ${payload.testId} passed!` };
});